package com.example.agrigreennewssys.controller;


import com.example.agrigreennewssys.dto.ChangePasswordDTO;
import com.example.agrigreennewssys.entity.User;
import com.example.agrigreennewssys.entity.UserProfile;
import com.example.agrigreennewssys.service.UserProfileService;
import com.example.agrigreennewssys.service.UserService;
import com.example.agrigreennewssys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserProfileService userProfileService;
    @Autowired
    private PasswordEncoder passwordEncoder;

//    @GetMapping("/register")
//    public String showRegisterForm(Model model) {
//        model.addAttribute("user", new User());
//        return "register";
//    }

    @PostMapping("/register")
    public Result register(@RequestBody User registerUser) {
        // 检查用户名是否已存在
        User existingUser = userService.findByUsername(registerUser.getUsername());
        if (existingUser != null) {
           return Result.error(400, "用户名已存在");
        }
        registerUser.setRole("USER");
        // 注册新用户
        userService.register(registerUser);
        return Result.success();
    }
    // 获取所有用户
     @GetMapping("/users")
     public Result getAllUsers() {
          List<User> users = userService.findAll();
          return Result.success(users);
     }
     @DeleteMapping( "/user/{id}")
     public Result deleteUser(@PathVariable Long id) {
         boolean deleted = userService.deleteUser(id);
         if (deleted) {
             return Result.success();
         }
         return Result.error(400, "用户不存在");
     }
    // 修改用户角色
    @PutMapping("/user/{userId}/role")
    public Result updateUserRole(@PathVariable Long userId, @RequestParam String role) {
        userService.updateUserRole(userId, role);
        return Result.success();
    }

    /**
     *  修改密码
     * @param changePasswordDTO
     * @return
     */
    @PutMapping("/user/password")
    public Result changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) {
        User user = userService.judgePassword(changePasswordDTO);
        if (user == null){
            return Result.error(400, "当前密码错误");
        }
        userService.updatePassword(changePasswordDTO.getUserId(), changePasswordDTO.getNewPassword());
        return Result.success();
    }
    @GetMapping("/user/profile/{userId}")
    public Result getUserProfile(@PathVariable Long userId) {
        UserProfile userProfile = userProfileService.getUserProfileByUserId(userId);
        return Result.success(userProfile);
    }

    /**
     * 修改用户简介
     * @param userProfile
     * @return
     */
    @PutMapping("/user/profile")
    public Result updateUserProfile(@RequestBody UserProfile userProfile) {
        userProfileService.updateUserProfile(userProfile);
        return Result.success();
    }

}
